﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RBS.Common;
using RBS.Common.JsonEntity;
using RBS.Web.Controllers;
using Webdiyer.WebControls.Mvc;

namespace RBS.Web.Areas.User.Controllers
{
    public class CollectController : BaseController
    {
        //
        // GET: /User/Collect/

        //酒店收藏列表
        public ActionResult CollectHotelList(int id = 1, string kword = null)
        {
            const int pageSize = 10;
        
            var bll = new RBS.Core.BLL.U_Collect_Hotel();
            // var admin = GetAdminInfo(); //取得当前管理员信息
            if (kword != null) kword = kword.Replace("'", "");
            var strTemp = new StringBuilder();
            if (!string.IsNullOrEmpty(kword))
            {
                //strTemp.Append(" and (H_UID like  '%" + kword + "%' or H_UserName like '%" + kword + "%')");
            }
            string strWhere = " and UserId="+ CurrentUser.Id+ strTemp.ToString();
            const string orderby = " id  desc";
            var startIndex = id;

            int totalItems = 0; //要分页的总记录数
            DataTable dt = bll.GetList(pageSize, startIndex, strWhere, orderby, out totalItems).Tables[0];
            //PagedList构造函数 
            var list = IListDataSet.DataTableToIList<RBS.Core.Model.U_Collect_Hotel>(dt);
            PagedList<RBS.Core.Model.U_Collect_Hotel> arts = null;
            if (list != null)
                arts = new PagedList<RBS.Core.Model.U_Collect_Hotel>(list, id, pageSize, totalItems);
            else
            {
                list = new List<RBS.Core.Model.U_Collect_Hotel>();
                arts = new PagedList<RBS.Core.Model.U_Collect_Hotel>(list, id, pageSize, totalItems);
            }
            return View(arts);
        }

        //收藏酒店  
        public JavaScriptResult AddHotle(int hotelid)
        {
            var bll = new RBS.Core.BLL.U_Collect_Hotel();
           DataSet dataSet = bll.GetList("HotelId= " + hotelid);

            if (dataSet.Tables[0].Rows.Count > 0)
            {
                var script = "$.dialog.alert('你已经收藏过该酒店');";
                return JavaScript(script); 
            }
            var model = new RBS.Core.Model.U_Collect_Hotel
            {
                UserId = CurrentUser.Id,
                CollectTime = DateTime.Now,
                HotelId = hotelid
            };

            if (bll.Add(model) > 0)
            {
                var script = "$.dialog.alert('收藏成功');"; 
                return JavaScript(script); 
            }
            return JavaScript("");

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult CollectHotelDelete(string ids, string returnUrl)
        {
            var ret = new JsonReturnMessages();
     
            try
            {
                foreach (var id in ids.TrimEnd(',').Split(',').ToList())
                {
                    new RBS.Core.BLL.U_Collect_Hotel().Delete(int.Parse(id));
                }
            }
            catch (Exception exception)
            {
                ret.IsSuccess = false;
                ret.Msg = "操作失败！";
                return Json(ret);
            } 
            ret.IsSuccess = true;
            ret.Msg = "操作成功";
            ret.Data = !string.IsNullOrEmpty(returnUrl) ? returnUrl : Url.Action("CollectHotelList", "Collect");
            return Json(ret);
        }
    }
}
